<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta http-equiv="X-UA-Compatible" content="IE=edge">
		<meta name="format-detection" content="telephone=no" />
		<meta name="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
		<script src="../../script/mui.min.js"></script>
		<script src="../../script/jquery.min.js"></script>
		<link rel="stylesheet" href="../../css/mui.min.css" />
		<link rel="stylesheet" href="../../css/swiper-3.4.2.min.css">
		<link rel="stylesheet" type="text/css" href="../../css/weui.min.css">
		<link rel="stylesheet" href="../../css/base.css"/>
		<link rel="stylesheet" href="../../css/exam.css">
		<link rel="stylesheet" type="text/css" href="../../css/people_diaocha.css"/>
		<link rel="stylesheet" type="text/css" href="../../css/singlePeo_diaocha.css"/>
		<title>学生端投票结果页</title>
		<script type="text/javascript" charset="utf-8">
			mui.init();
		</script>
		<style>
			html {
				font-size: 10px;
			}
			#question_div {
				height: 400px
			}
			.weui_mask {
				position: fixed;
				z-index: 998;
				top: 0;
				right: 0;
				bottom: 0;
				left: 0;
			}
			.weui_dialog {
				z-index: 999;
			}
		</style>
	</head>
	<body >
		<div class="stu-test" id="scroll" style="display: none">
			<div class="space10"></div>
			<div class="k-con test-m-bottom70 test-analysis" id="question_div"></div>
			<div id="popover-card" class="mui-popover test-num-card" style="width: 95% ">
				<div class="mui-scroll-wrapper">
					<div class="mui-scroll" id="scroll-card"></div>
				</div>
			</div>
		</div>
		<div id="backAlert" class="weui_dialog_confirm" style="display: none">
			<div class="weui_mask"></div>
			<div class="weui_dialog">
				<div class="weui_dialog_hd">
					<strong class="weui_dialog_title" id="checkNum"></strong>
				</div>
				<div class="weui_dialog_bd"></div>
				<div class="weui_dialog_ft">
					<a  href="#" class="weui_btn_dialog default" id="closeTest_no" style="color:#18b4ed">取消</a>
					<a  href="#" class="weui_btn_dialog primary" id="closeTest_yes" style="color:#18b4ed">确定</a>
				</div>
			</div>
		</div>
		<div class="noSign" hidden="hidden"  id="noData">
			<img src="../../image/icon_img.png" width="60%">
			<p class="font16 top_15 base_dark_grey" >
				暂无数据
			</p>
		</div>
		<div class="loadFailure" hidden="hidden" id="noNet">
			<img src="../../image/icon_img01.png" width="60%">
			<p class="" >
				数据加载失败
			</p>
			<p>
				请检查您的网络
			</p>
			<button type="button"  onclick="loadVoteData()"class="mui-btn mui-btn-outlined">
				重新加载
			</button>
		</div>
	</body>
	<script type="text/javascript" src="../../script/api.js"></script>
	<script type="text/javascript" src="../../script/MD5.js"></script>
	<script src="../../script/jquery.min.js" type="text/javascript"></script>
	<script type="text/javascript" src="../../script/requestTool.js"></script>
	<script type="text/javascript" src="../../script/timeStampTool.js"></script>
	<script type="text/javascript" src="../../script/echarts.min.js"></script>
	<script src="../../script/common.js"></script>
	<script type="text/javascript">
		var itemNameArr = new Array();
		var colorList = new Array();
		//0是开始,1是结束
		var back_type = "";
		//0是正常退出，1是点击关闭投票后退出
		var vote_type = 0;
		//1是当前投票处于关闭状态,默认未关闭
		apiready = function() {
			preData();
			loadVoteData()
			btnClick();
			ReceNotifi();
			setRefreshHeader();
			sendBuriedNotifi('0', '学生端投票结果页', '190');
		}
		function sendBuriedNotifi(busionessid, pagetype, pagetypeid) {
			var userinfo = {
				uid : $api.getStorage('cfnetppuid'),
				jid : $api.getStorage('cfnetppjxid'),
				uname : $api.getStorage('cfnetppusername'),
			}
			var data = {
				busionessid : busionessid,
				pagetype : pagetype,
				pagetypeid : pagetypeid,
				sourceurl : api.pageParam.sourceurl,
				sourcetitle : api.pageParam.sourcetitle,
				accesspageurl : window.location.pathname,
				accesspagetitle : document.title,
				userinfo : userinfo
			}
			SendBehaviorTrajectoriesNotifi_Study(data);
		}

		//下拉刷新
		function setRefreshHeader() {
			api.setRefreshHeaderInfo({
				visible : true,
				bgColor : '#ccc',
				textColor : '#fff',
				textDown : '下拉刷新...',
				textUp : '松开刷新...',
				showTime : true
			}, function(ret, err) {
				//				$('#question_div').html('')
				loadVoteData();
			});
		}

		//接收推送通知
		function ReceNotifi() {
			api.addEventListener({
				name : 'openVoteScorePopu'
			}, function(ret, err) {
				//coding...
				backToast();
			});
			api.addEventListener({
				name : 'pushMessage_classroomMessage'
			}, function(ret, err) {
				var id = ret.value.id;
				if (id == api.pageParam.iid) {
					var status = ret.value.status;
					var VOTE_TYPE = ret.value.type;
					vote_type = status == 2 ? 1 : 0;
					if (VOTE_TYPE == 10057) {
						if (status == 3 || status == 2) {//投票更新或者投票结束
							loadVoteData();
						}
					}
				}
			});
		}

		//预加载数据
		function preData() {
			if (vote_type == 0) {//投票未关闭
				if (api.pageParam.template == 0) {
					itemNameArr = ['赞成', '反对']
				} else {
					itemNameArr = ['是', '否']
				}
				colorList = ['#68c9ed', '#ed6666'];
			} else {
				if (api.pageParam.template == 0) {
					itemNameArr = ['赞成', '反对', '未参与']
				} else {
					itemNameArr = ['是', '否', '未参与']
				}
				colorList = ['#68c9ed', '#ed6666', '#c6c6c6'];
			}
		}

		//按钮点击事件
		function btnClick() {
			//正常情况下是否退出当前考试按钮取消键
			$('#closeTest_no').click(function() {
				$('#backAlert').hide();
			});
			//正常情况下是否退出当前考试按钮确定键
			$('#closeTest_yes').click(function() {
				$('#backAlert').hide();
				back();
			});
		}

		//退出提示
		function backToast() {
			if (api.pageParam.ismocc == "history") {//历史记录列表点击进入
				api.closeWin({
				});
			} else {
				$('#backAlert').show();
				$('#checkNum').text('是否确定退出当前投票结果页面');
				back_type = 1;
			}
		}

		//关闭投票
		function closeVote() {
			backToast()
		}

		//获取数据
		function loadVoteData() {
			var params = {
				iid : api.pageParam.iid,
				jid : $api.getStorage('cfnetppjxid'),
				tid : 6,
			}
			cfnetppPOST(url_getDetailsOfVoting, params, false, function(data, status) {
				api.refreshHeaderLoadDone();
				hideProgress();
				if (status == 'success') {
					if (data.code == 200) {
						if (!isObjectEmpty(data)) {
							$('#scroll').show();
							vote_type = judgeVoteType(data.data);
							//返回数量为3,即视为当前活动已结束状态
							var numArr = getChartData(data.data);
							if (vote_type == 1) {//已结束
								preData();
							}
							setupChart(numArr)
						} else {
							$('#scroll').hide();
							$('#noNet').hide();
							$('#noData').show();
						}
					} else {
						$('#scroll').hide();
						$('#noNet').hide();
						$('#noData').show();
					}
				} else {
					$('#scroll').hide();
					$('#noData').hide();
					$('#noNet').show();
				}
			})
		}

		//判断当前投票状态
		function judgeVoteType(data) {
			var str = 0;
			for (var i = 0; i < data.length; i++) {
				if (data[i].type == 1) {//未参与
					str = 1;
				}
			}
			return str;
		}

		//组合数据
		function getChartData(data) {
			var dataArr = new Array();
			if (vote_type == 0) {
				dataArr = [0, 0];
			} else {
				dataArr = [0, 0, 0];
			}
			for (var i = 0; i < data.length; i++) {
				if (data[i].type == 2) {//赞成
					dataArr[0] = data[i].total
				}
				if (data[i].type == 3) {//反对
					dataArr[1] = data[i].total
				}
				if (vote_type == 1) {
					if (data[i].type == 1) {//未参与
						dataArr[2] = data[i].total
					}
				}
			}
			return dataArr;
		}

		//设置柱状图
		function setupChart(numArr) {
			var myChart = echarts.init(document.getElementById('question_div'));
			var maxnum = "";
			var showStaus = false;
			//设置y轴最大数
			if (Math.max.apply(null, numArr) > 5) {
				maxnum = Math.max.apply(null, numArr);
			} else {
				maxnum = 5;
			}
			//如果没有参与，则不显示“人”提示符
			if (Math.max.apply(null, numArr) > 0) {
				showStaus = true;
			} else {
				showStaus = false;
			}
			var option1 = {
				tooltip : {
					//					trigger : 'axis',
					axisPointer : {// 坐标轴指示器，坐标轴触发有效
						type : 'shadow' // 默认为直线，可选为：'line' | 'shadow'
					}
				},
				xAxis : {
					type : 'category',
					data : itemNameArr
				},
				yAxis : {
					type : 'value',
					name : "人数",
					minInterval : 1,
					max : maxnum,
					splitLine : {
						show : false
					},
				},
				series : [{
					data : numArr,
					type : 'bar',
					name : '人数',
					barWidth : '60%',
					label : {
						normal : {
							show : showStaus,
							position : 'top',
							formatter : '{c}人',
							textStyle : {
								color : 'black'
							}
						}
					},
					itemStyle : {
						normal : {
							color : function(params) {
								return colorList[params.dataIndex]
							}
						}
					}
				}]
			};
			var option = option1;
			myChart.setOption(option);
		}

		function back() {
			api.sendEvent({
				name : 'closeVoteScorePopu'
			});
		}
	</script>
</html>